#!/usr/bin/env bash
# set -x
host=$1
project="wechat_token_api"
project_port="9041"
project_host="0.0.0.0"
project_images="wechat_token_api_images"

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $project main.go

#version="`date +%Y%m%d%H`"
version="latest"
echo "version: $version"

ssh root@$host > /dev/null 2>&1 << eeooff
  mkdir -p /root/release/$project
  mkdir -p /root/release/$project
  mkdir -p /data/wechat_token_api
  exit
eeooff

scp $project root@$host:/root/release/$project/
scp app.prod.yml root@$host:/root/release/$project/app.yml
scp Dockerfile root@$host:/root/release/$project/

rm -f $project

ssh root@$host > /dev/null 2>&1 << eeooff
  cd /root/release/$project
  chmod 777 $project
  docker build -t $project_images:latest -f ./Dockerfile .
  rm -f app.yml
  if docker ps | grep $project | awk '{print \$NF}' | grep $project ; then
    docker stop $project
    docker rm $project
  fi
  docker run --name $project -p $project_host:$project_port:$project_port -v /data/wechat_token_api/store:/store -v /data/wechat_token_api/logs:/logs -itd --rm $project_images:latest
#  docker images -a | grep none | awk '{print $3}' | xargs docker rmi
  exit
eeooff
echo "$project 更新完成!"
echo "`date +%Y%m%d%H`"